//
//  CAAnimationGroupViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/26.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "CAAnimationGroupViewController.h"

@interface CAAnimationGroupViewController ()

@property (nonatomic, strong) UIView *backgroundView;

@property (nonatomic, strong) CALayer *rectLayer;

@property (nonatomic, strong) UIBezierPath *bezierpath;

@end

@implementation CAAnimationGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
    self.backgroundView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:_backgroundView];
    
    self.bezierpath = [[UIBezierPath alloc] init];
    [self.bezierpath moveToPoint:CGPointMake(30, 160)];
    [self.bezierpath addCurveToPoint:CGPointMake(330, 160) controlPoint1:CGPointMake(105, 10) controlPoint2:CGPointMake(255, 310)];
    
    
    CAShapeLayer *pathLayer = [CAShapeLayer layer];
    pathLayer.strokeColor = [UIColor cyanColor].CGColor;
    pathLayer.fillColor = [UIColor clearColor].CGColor;
    [pathLayer setLineWidth:2.0f];
    [pathLayer setLineCap:@"kCALineCapRound"];
    pathLayer.path = self.bezierpath.CGPath;
    [self.backgroundView.layer addSublayer:pathLayer];
    
    self.rectLayer = [CALayer layer];
    self.rectLayer.bounds = CGRectMake(0, 0, 50, 50);
    self.rectLayer.backgroundColor = [UIColor redColor].CGColor;
    self.rectLayer.position = CGPointMake(30, 160);
    [self.backgroundView.layer addSublayer:self.rectLayer];
    
    UIButton *startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    startButton.frame = CGRectMake(50, 400, 150, 30);
    [startButton setTitle:@"开始" forState:UIControlStateNormal];
    [startButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [startButton addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:startButton];
    
    
    UIButton *recoverButton = [UIButton buttonWithType:UIButtonTypeCustom];
    recoverButton.frame = CGRectMake(50, 450, 150, 30);
    [recoverButton setTitle:@"恢复原状态" forState:UIControlStateNormal];
    [recoverButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [recoverButton addTarget:self action:@selector(recover:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:recoverButton];
}

- (void)start:(UIButton *)button
{
    
    if ([self.rectLayer animationForKey:@"animationGroup"]) {
        [self.rectLayer removeAnimationForKey:@"animationGroup"];
    }
    // 动画一
    // 关键帧动画
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyAnimation.duration  = 2.0;
    keyAnimation.path = self.bezierpath.CGPath;
    keyAnimation.rotationMode = kCAAnimationRotateAuto; // 沿着轨迹的切线进行移动
    
    // 动画二
    CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
    baseAnimation.toValue = (__bridge id _Nullable)([UIColor yellowColor].CGColor);
    
    // 动画组
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    animationGroup.animations = @[keyAnimation,baseAnimation];
    animationGroup.duration = 2.0;
    animationGroup.fillMode = kCAFillModeForwards;
    animationGroup.removedOnCompletion = NO;
    [self.rectLayer addAnimation:animationGroup forKey:@"animationGroup"];
    
}

- (void)recover:(UIButton *)button
{
    // 我们在使用fillModel和removedOnCompletion的时候，一定要记住要手动释放，否则动画只有在图层消失的时候才会移除，并且如果直接设置position属性是没有任何作用的，因为动画还保留之前的填充状态
    if ([self.rectLayer animationForKey:@"animationGroup"]) {
        [self.rectLayer removeAnimationForKey:@"animationGroup"];
        self.rectLayer.position = CGPointMake(30, 160);
    }
    
    // self.rectLayer.position = CGPointMake(30, 160); // 直接这样使用是没有作用的
}

@end
